import { createRouter, createWebHistory } from 'vue-router'

import routes from './constRoutes'
import nProgress, { NProgress } from 'nprogress'
import "nprogress/nprogress.css"

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

  //前置守卫
  router.beforeEach((to,from,next)=>{
    //获取token
    let token = localStorage.getItem('token')
    if( token ){ //已登录
      if( to.path == '/Login' ){ // 想要去往登录页
        next('/');
      }else{ //想要去往其他页
        next();
      }
    }else{ //未登录
      if( to.path == '/Login' || to.path == '/Register' ){ // 想要去往登录页/注册页
        next();
      }else{ //想要去往其他页
        next('/Login');
      }
    }

    //动态修改网页标题
    document.title = to.meta.title;

    //开启进度条动画
    nProgress.start()

  })

  //后置守卫
  router.afterEach((to,from)=>{

    //关闭进度条动画
    nProgress.done();

  })


export default router
